package ch.zbw.rre.queue;

import javax.swing.text.Position;

public class QueueArray implements Queue{

	private final int MAX = 4;
	private int counter;
	private int tailPointer;
	private int headPointer;
	private Node[] queue;
	private Node head;

	public QueueArray(){
		this.counter = 0;
		this.tailPointer = 0;
		this.headPointer = 0;
		this.queue = new Node[MAX];
		this.head = new Node();
	}
	
	
	@Override
	public boolean enq(Element obj) {
		if(this.counter == 0){
			Node tempHead;
			tempHead = new Node();
			tempHead.setItem(obj);
			tempHead.setNext(head);
			counter ++;
			this.queue[0] = tempHead;
			this.head = queue[0];
			this.tailPointer = 1;
			this.headPointer = 0;
			return true;
		}
		if((this.counter >=1) && (this.counter < MAX)){
			Node tempHead;
			tempHead = new Node();
			tempHead.setItem(obj);
			System.out.println("resultat: " + (this.tailPointer+1)% MAX);
			
			if(((this.tailPointer +1) % MAX) >= 1){
				queue[tailPointer] = tempHead;
				this.tailPointer ++;
				this.counter ++;
				return true;
			}
			
			
			if(((this.tailPointer +1) % MAX) == 0){
				this.queue[tailPointer] = tempHead;
				this.tailPointer =0;
				this.counter ++;
				return true;
			}
		}
		return false;
	}

	
	
	@Override
	public Element deq() {
		
		if(this.empty() == false){
			if((headPointer % MAX) >= 1){
				Element tempHead;
				tempHead = this.queue[headPointer].getItem();
				this.queue[headPointer] = null;
				
				this.headPointer ++;
				this.counter --;
				return tempHead;
			}
			if((headPointer % MAX) == 0){
				Element tempHead;
				tempHead = this.queue[headPointer].getItem();
				this.queue[headPointer] = null;
				this.headPointer =1;
				this.counter --;
				return tempHead;
				
			}
			
		}
		return null;
	}

	
	@Override
	public boolean empty() {
		if(this.counter < 1){
			return true;
		}
		return false;
	}

}
